<?php
error_reporting(E_ALL);
include('classes/config.inc.php');
include(DIR_CLASS. 'Session.php');
include(DIR_CLASS. 'Mail.php');
include(DIR_CLASS. 'Database.php');
include(DIR_CLASS. 'TemplateParser.php');
include(DIR_CLASS. 'Form.php');

$session = new UserSession();
$session->open();

$captcha = new Captcha();
$captcha->open();

if($session->check())
	$menu = "<a href=\"profile.php\">Profil</a> / <a href=\"login.php?act=logout\">Logga ut</a>[". substr($session->getEmail(),0,12) ."] / <a href=\"\">Karta &ouml;ver hemsidan</a>";
else
	$menu = "<a href=\"register.php\">Registrera dig</a> / <a href=\"login.php\">Logga in</a> / <a href=\"sitemap.php\">Karta &ouml;ver hemsidan</a>";

	if ($session->check()){	
	//Ingelogd?? zoja naar index verwijzen.
	header('Location: index.php');
	die();
}

if(isset($_POST['action']) && $_POST['action'] == 'register'){
	// REGISTER POST
	$form = new Form();
	$formElements = array("name" => 1,"lname" => 1,"email" => 1,"captcha" => 1,"birthday" => 1);

	$form->open($formElements);

	if($form->isValidForm() && $captcha->check($form->get("captcha"))){
		//Registreren
		$random_pasw = substr(md5(rand()),0,8);
		
		$verify = "SELECT * FROM ". TABLE_USERS ." WHERE(email='". $form->get("email") ."')";
		$sql = "INSERT INTO ". TABLE_USERS ." (email, pass, family_name, name, birth_date) VALUES ('".$form->get("email")."','".$random_pasw."','".$form->get("lname")."','".$form->get("name")."','".date("Y-m-d",strtotime($form->get("birthday")))."')";
		$db = new MYSQLDatabase(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);
		try {
			$db->connect();
			$link = $db->query($verify);
			if($link!=false && mysql_num_rows($link)<=0){
				$link = $db->query($sql);
				if($link){
					$mailer = new Mail("Registratie","email : ".$random_pasw);
					$mailer->send($form->get("email"));
					if($mailer->isSended()){
						$error="You are successfully registered. Please wait on the email with your password.";
					}
					else{
						$error="The email with your password in it hasen't been send. Try resetting password.";
					}
				}
				else{
					$error="Error: You are NOT added to the system.";
				}
			}
			else {
				$error="Given email is already registered on this site.";
			}
		}catch (Exception $e){
			$error = "Database problems: ".$e->getMessage();
		}
	}
	else{
		//incorrecte data
		$error="No legal input or wrong answer!";
	}
	$captcha->save();
		
	$template_array = array(
		"MENU" => $menu,
		"ERROR" => $error,
		"CAPTCHA" => $captcha->getQuestion(),
		"NAME"=> $form->get("name"),
		"LNAME" => $form->get("lname"),
		"EMAIL" => $form->get("email"),
		"BIRTHDAY" => date("d-m-Y"),
	);
	$template = new TemplateParser();
	$template->loadfile("templates/register.tpl");
	$template->assignVars($template_array);
	$template->output();
}
else{
	$captcha = new Captcha();
	$captcha->open();
	$captcha->save();
	
	$template_array = array(
			"MENU" => $menu,
			"ERROR" => "",
			"CAPTCHA" => $captcha->getQuestion(),
			"NAME"=> "",
			"LNAME" => "",
			"EMAIL" => "",
			"BIRTHDAY" => date("d-m-Y"),
		);
		
	$template = new TemplateParser();
	$template->loadfile("templates/register.tpl");
	$template->assignVars($template_array);
	$template->output();
}
?>